//
//  EntryViewController.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/1.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "EntryViewController.h"
#import "AnswersTableViewCell.h"
#import "CanHelpPeopleCollectionViewCell.h"
#import "ImagePlayerView.h"
#import "JobDetailVC.h"

@interface EntryViewController ()<UITableViewDelegate,UITableViewDataSource,ImagePlayerViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic)  NSMutableArray *arrData;
@property (strong, nonatomic) IBOutlet UIView *headView;
#pragma mark ------广告位
@property (nonatomic, strong)  NSArray *imageURLs;
@property (weak, nonatomic) IBOutlet ImagePlayerView *headImageView;

#pragma mark ------综合匹配
@property (weak, nonatomic) IBOutlet UIView *selectJobView1;
@property (weak, nonatomic) IBOutlet UILabel *jobNameLab1;
@property (weak, nonatomic) IBOutlet UILabel *selectJobNameLab1;

@property (weak, nonatomic) IBOutlet UIView *selectJobView2;
@property (weak, nonatomic) IBOutlet UILabel *jobNameLab2;
@property (weak, nonatomic) IBOutlet UILabel *selectJobNameLab2;

@property (weak, nonatomic) IBOutlet UIView *selectJobView3;
@property (weak, nonatomic) IBOutlet UILabel *jobNameLab3;
@property (weak, nonatomic) IBOutlet UILabel *selectJobNameLab3;

#pragma mark ------谁能帮我
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic)  NSMutableArray *helpArrDate;//谁能帮我数组
@end

@implementation EntryViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"首页";
    self.navigationController.navigationBarHidden = YES;
    
    /**J下载广告*/
    [self loadAdView];
    
    /**加载列表*/
    [self loadTabbleView];
    
    /**加载谁能帮我*/
    [self loadCollectionView];
    

    [self downRefreshRequest];
    [self upRefreshRequest];
}
/*
 方法描述:
 广告位
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadAdView{
     self.headImageView.pageControlPosition = ICPageControlPosition_BottomCenter;
    self.imageURLs = @[@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1519967481931&di=98eb63b8f0f1bd0a886adb808c146bcb&imgtype=0&src=http%3A%2F%2Fopen.189.cn%2Fdiscuz%2Fdata%2Fattachment%2Fforum%2F201309%2F03%2F103102epvbrvcz67bxrcsp.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1519967292781&di=5589ce8451dd63fa6d9cd832a82ac33b&imgtype=0&src=http%3A%2F%2Fwenwen.soso.com%2Fp%2F20111116%2F20111116123438-407789595.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1519967481931&di=98eb63b8f0f1bd0a886adb808c146bcb&imgtype=0&src=http%3A%2F%2Fopen.189.cn%2Fdiscuz%2Fdata%2Fattachment%2Fforum%2F201309%2F03%2F103102epvbrvcz67bxrcsp.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1519967481931&di=98eb63b8f0f1bd0a886adb808c146bcb&imgtype=0&src=http%3A%2F%2Fopen.189.cn%2Fdiscuz%2Fdata%2Fattachment%2Fforum%2F201309%2F03%2F103102epvbrvcz67bxrcsp.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1519967481931&di=98eb63b8f0f1bd0a886adb808c146bcb&imgtype=0&src=http%3A%2F%2Fopen.189.cn%2Fdiscuz%2Fdata%2Fattachment%2Fforum%2F201309%2F03%2F103102epvbrvcz67bxrcsp.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1519967481931&di=98eb63b8f0f1bd0a886adb808c146bcb&imgtype=0&src=http%3A%2F%2Fopen.189.cn%2Fdiscuz%2Fdata%2Fattachment%2Fforum%2F201309%2F03%2F103102epvbrvcz67bxrcsp.jpg",@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1519967481931&di=98eb63b8f0f1bd0a886adb808c146bcb&imgtype=0&src=http%3A%2F%2Fopen.189.cn%2Fdiscuz%2Fdata%2Fattachment%2Fforum%2F201309%2F03%2F103102epvbrvcz67bxrcsp.jpg"];
    
    self.headImageView.imagePlayerViewDelegate = self;
    [self.headImageView reloadData];
}
/*
 方法描述:
 加载列表
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadTabbleView{
    self.headView.frame = CGRectMake(0, 0, kScreenWidth, 909);
    self.tableView.tableHeaderView = self.headView;
    [self.tableView registerNib:[UINib nibWithNibName:@"AnswersTableViewCell" bundle:nil] forCellReuseIdentifier:@"AnswersTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 184.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
}
/*
 方法描述:
 谁能帮我
 
 参数说明:
 <#参数说明#>
 
 返回结果:
 <#返回结果#>
 
 */
-(void)loadCollectionView{
    [self.collectionView registerNib:[UINib nibWithNibName:@"CanHelpPeopleCollectionViewCell" bundle:nil]  forCellWithReuseIdentifier:@"CanHelpPeopleCollectionViewCell"];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 15;
    flowLayout.minimumLineSpacing = 15;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsZero;
    flowLayout.itemSize = CGSizeMake(120,158);
    flowLayout.sectionInset = UIEdgeInsetsMake(0,15, 0, 15);
    self.collectionView.collectionViewLayout = flowLayout;
    self.helpArrDate = [NSMutableArray array];
}

#pragma mark - ImagePlayerViewDelegate 广告代理
- (NSInteger)numberOfItems
{
    return self.imageURLs.count;
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView loadImageForImageView:(UIImageView *)imageView index:(NSInteger)index
{
    // recommend to use SDWebImage lib to load web image
    NSString *imgUrl = [self.imageURLs objectAtIndex:index];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:IMAGE(@"默认图")];
}

- (void)imagePlayerView:(ImagePlayerView *)imagePlayerView didTapAtIndex:(NSInteger)index
{
}
#pragma mark ---- UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return  11;self.helpArrDate.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"CanHelpPeopleCollectionViewCell";
    CanHelpPeopleCollectionViewCell *cell  = [collectionView dequeueReusableCellWithReuseIdentifier: CellIdentifier forIndexPath:indexPath];
    cell.contentView.backgroundColor = UIColorFromRGB(0xF2F5FA);
    
    
    return cell;
    
}

#pragma mark ------tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;self.arrData.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 65;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    static NSString *identifier = @"AnswersTableViewCell";
    AnswersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
//    MessageModel *model = self.arrData[indexPath.row];
//    cell.titleName.text = model.title;
    return cell;
    
}


- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark 下拉刷新
- (void)downRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        
        weakSelf.pageNumber = kStartPage;
        
//        [YJMSendRequest getUserMessageWithPageNo:[NSString stringWithFormat:@"%zd",weakSelf.page] result:^(NSDictionary *result, NSError *error) {
//            result = [CommonUtils decryptDicWithResult:result];
//            if([result[@"result"] integerValue] == 1)
//            {
//                weakSelf.tableView.emptyDataSetDelegate = weakSelf;
//                weakSelf.tableView.emptyDataSetSource = weakSelf;
//                
//                NSArray * listDictionaries = result[@"data"][@"list"];
//                weakSelf.arrData  = [NSMutableArray array];
//                for(NSDictionary * listDictionary in listDictionaries){
//                    MessageModel * listItem = [[MessageModel alloc] initWithDictionary:listDictionary];
//                    [weakSelf.arrData addObject:listItem];
//                }
//                
//                
//                [weakSelf.tableView reloadData];
//                if (weakSelf.arrData.count < kNumOfPageIntSize) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
//
//                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
//                }
                [weakSelf.tableView.mj_header endRefreshing];
//
//                
//                
//            }else{
//                [weakSelf.tableView.mj_header endRefreshing];
//                [weakSelf.tableView.mj_footer endRefreshing];
//                [CommonUtils showToast:[result objectForKey:@"msg"]];
//            }
//        }];
        
    }];
    MJHEADER;
    
    
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark 上拉加载
- (void)upRefreshRequest //内部方法改
{
    __weak typeof(self) weakSelf = self;
    
    MJRefreshBackGifFooter *footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        weakSelf.pageNumber ++;
        
//        [YJMSendRequest getUserMessageWithPageNo:[NSString stringWithFormat:@"%zd",weakSelf.page]   result:^(NSDictionary *result, NSError *error) {
//            result = [CommonUtils decryptDicWithResult:result];
//            if([result[@"result"] integerValue] == 1)
//            {
//                weakSelf.tableView.emptyDataSetDelegate = weakSelf;
//                weakSelf.tableView.emptyDataSetSource = weakSelf;
//
//                NSArray * listDictionaries = result[@"data"][@"list"];
//                for(NSDictionary * listDictionary in listDictionaries){
//                    MessageModel * listItem = [[MessageModel alloc] initWithDictionary:listDictionary];
//                    [weakSelf.arrData addObject:listItem];
//                }
//                [weakSelf.tableView reloadData];
//                /**当前数量少于10条时就无下一页*/
//                if (listDictionaries.count < kNumOfPageIntSize) {
//                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
//
//                }else{
//                    [weakSelf.tableView.mj_footer endRefreshing];
//                }
//                [weakSelf.tableView.mj_header endRefreshing];
//            }else{
//                [weakSelf.tableView.mj_header endRefreshing];
//                [weakSelf.tableView.mj_footer endRefreshing];
//                [CommonUtils showToast:[result objectForKey:@"msg"]];
//            }
//        }];
        
    }];
    
    MJFOOTER;
    self.tableView.mj_footer = footer;
  
}

#pragma mark ------综合匹配

- (IBAction)jobButtonClick:(UIButton *)sender {
    switch (sender.tag) {
        case 1:
        {
            self.selectJobView1.hidden = NO;
            self.selectJobView2.hidden = YES;
            self.selectJobView3.hidden = YES;
        }
            break;
        case 2:
        {
            self.selectJobView1.hidden = YES;
            self.selectJobView2.hidden = NO;
            self.selectJobView3.hidden = YES;
        }
            break;
        case 3:
        {
            self.selectJobView1.hidden = YES;
            self.selectJobView2.hidden = YES;
            self.selectJobView3.hidden = NO;
        }
            break;
        default:
            break;
    }
}
- (IBAction)jobDetailClick:(UIButton *)sender {
    JobDetailVC *vc = [[JobDetailVC alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)dealloc
{
    // clear
    [self.headImageView stopTimer];
    self.headImageView.imagePlayerViewDelegate = nil;
    self.headImageView = nil;
}
@end
